<?php

declare(strict_types=1);

namespace app\admin\controller;

use app\admin\BaseAdminController;
use think\facade\View;
use think\facade\Cookie;
use app\system\model\LogLogin as Log;

class Dashboard extends BaseAdminController
{

    /**
     * 后台首页
     */
    public function index()
    {
        View::assign('title', '后台首页');
        // 统计数据 Widgets
        $widgets = $this->trigger('DashboardWidget');
        View::assign('widgets', $widgets ?: []);
        // 列表内容 Widgets
        $contents = $this->trigger('DashboardContent');
        View::assign('contents', $contents ?: []);
        // 加载图表 JS
        View::assign('addJs', addJs(['chart']));
        return View::fetch();
    }

    /**
     * 清理系统缓存
     */
    public function clean()
    {
        $this->deldir(root_path() . 'runtime/');
        $this->trigger('CleanCache');
        return json(['msg' => '']);
    }

    /**
     * 遍历目录并删除缓存文件, 不删除目录
     * @param string $dir 目录路径
     */
    private function deldir($dir)
    {
        $dh = opendir($dir);
        while ($file = readdir($dh)) {
            if ($file != "." && $file != "..") {
                $fullpath = $dir . "/" . $file;
                if (!is_dir($fullpath)) {
                    unlink($fullpath);
                } else {
                    $this->deldir($fullpath);
                }
            }
        }
        closedir($dh);
    }

    /**
     * 退出登录
     */
    public function logout()
    {
        Log::add(USERNAME, '退出成功');
        Cookie::delete('auth');
        return redirect(url('admin/index/index')->build());
    }

    /**
     * 无权访问
     */
    public function message()
    {
        View::assign('title', '无权访问');
        return View::fetch();
    }

    /**
     * 404 未找到页面
     */
    public function message404()
    {
        View::assign('title', '未找到页面');
        return View::fetch();
    }

    /**
     * 下载excel
     */
    public function download()
    {
        $file = input('file', '');
        $n = 1;
        if ($file) {
            $path = public_path() . 'temp/excel/';
            $files = glob($path . '*');
            $file = $path . $file;
            if (in_array($file, $files)) {
                header('Content-Description: File Transfer');
                header('Content-Type: application/vnd.ms-excel');
                header('Content-Disposition: attachment; filename="' . basename($file) . '"');
                header('Expires: 0');
                header('Cache-Control: must-revalidate');
                header('Pragma: public');
                header('Content-Length: ' . filesize($file));
                ob_clean();
                flush();
                readfile($file);
                unlink($file);
                $n = 0;
            }
        }
        if ($n) {
            return redirect(url('admin/dashboard/index')->build());
        }
    }

    /**
     * 下载excel (超级管理员模式)
     */
    public function superDownload()
    {
        $file = input('file', '');
        $mode = input('mode', '');
        $n = 1;
        if ($file && $mode && UID == 1) {
            $path = root_path() . 'runtime/' . $mode . '/';
            $specialChars = "#@$%^&*()_+-=[]{}`~<>?,/\\";
            $file = str_replace(str_split($specialChars), '', $file);
            $file = $path . 'temp/' . $file;
            if (file_exists($file) && is_file($file)) {
                header('Content-Description: File Transfer');
                header('Content-Type: application/vnd.ms-excel');
                header('Content-Disposition: attachment; filename="' . basename($file) . '"');
                header('Expires: 0');
                header('Cache-Control: must-revalidate');
                header('Pragma: public');
                header('Content-Length: ' . filesize($file));
                ob_clean();
                flush();
                readfile($file);
                $n = 0;
            }
        }
        if ($n) {
            return redirect(url('admin/dashboard/index')->build());
        }
    }
}
